import { CommonApi } from "@/api/managementCenter/projectManage/commonApi"
import { DataAcquisition } from "@/mockData/dataAcquisition.js"
import { ManagementCenterData } from "@/mockData/managementCenterData.js"
import { SessionCommon } from "./sessionCommon"
import { isOpenPermission, Utility } from "./utility"
/**
 * 其它公用方法，这里是关于业务的，不设置一些js的公用方法，js公用方法写在utility里面
 */
export const OtherCommon = {
	/**
	 * 获取当前菜单的层级
	 * @returns
	 */
	getCurrentMenuInfo() {
		let menuList = SessionCommon.getAllMenuList()
		let menuInfo = SessionCommon.getCurrentMenuInfo()
		let newList = []
		if (menuList && menuInfo) {
			menuList = JSON.parse(menuList)
			menuInfo = JSON.parse(menuInfo)
			newList.push(menuInfo)
			// 获取当前菜单的层级关系
			const getMenu = (cMenuInfo, cMenuList) => {
				let menuParentInfo = cMenuList.find((t) => t.id == cMenuInfo.parentId)
				if (typeof menuParentInfo != "undefined") {
					newList.push(menuParentInfo)
					getMenu(menuParentInfo, menuList)
				}
			}
			getMenu(menuInfo, menuList)
		}
		if (newList.length > 0) {
			newList = Utility.arrayCascade(
				newList,
				(c, p) => c.parentId == p.id,
				"children"
			)
			newList = Utility.arrayFlat(newList, (t) => t.children)
		}
		return newList
	},

	/**
	 * 获取所有的菜单list (这里不区分采集中心和管理中心)
	 */
	async getAllMenu(module = "") {
		let menuList = sessionStorage.getItem("ALLMENULIST")
		console.log(menuList, "menuList")
		if (menuList) {
			return JSON.parse(menuList)
		}
		let newData = []
		if (isOpenPermission) {
			let managementCenter = await CommonApi.getUserPermissionByToken(
				"managementCenter"
			)
			managementCenter = Utility.arrayFlat(managementCenter, (t) => t.children)
			let dataAcquisition = await CommonApi.getUserPermissionByToken(
				"dataAcquisition"
			)
			dataAcquisition = Utility.arrayFlat(dataAcquisition, (t) => t.children)
			let finishData = []
			if (module == "managementCenter") {
				finishData = managementCenter.concat(dataAcquisition)
			} else {
				finishData = dataAcquisition.concat(managementCenter)
			}
			newData = finishData
		} else {
			const daMenu = Utility.arrayFlat(
				DataAcquisition.leftMenuData,
				(t) => t.children
			)
			const mcData = Utility.arrayFlat(
				ManagementCenterData.leftMenuData,
				(t) => t.children
			)
			let lastData = []
			if (module == "managementCenter") {
				lastData = mcData.concat(daMenu)
			} else {
				lastData = daMenu.concat(mcData)
			}
			newData = lastData
		}
		sessionStorage.setItem("ALLMENULIST", JSON.stringify(newData))
		return newData
	},
}
